<?php
namespace core\classes;

class Request
{
    private $client;
    private $token;
    private $email;
    private $password;
    private $authPoint;
    public function __construct($email, $password, $authPoint)
    {
        $this->client = new \GuzzleHttp\Client();
        $this->email = preg_replace("/\r\n/", '', trim($email));
        $this->password = preg_replace('/\r\n/i', '', trim($password));
        $this->authPoint = preg_replace('/\r\n/i', '', trim($authPoint));
    }
    public function login()
    {
        $response = $this->client->request(
            "POST",
            $this->authPoint . "/api/vue-element-admin/user/login",
            [
                'form_params' => [
                    'username' => $this->email, 'password' => $this->password, 'device_name' => 'client'
                ]
            ]
        );
        $data =  json_decode($response->getBody()->getContents(), true);
        if ($data != null && $data['code'] == 20000) {
            $this->token = $data['data']['token'];
            return $data;
        } else {
            return false;
        }
    }
    public function getClientList()
    {
        $response = $this->client->request(
            "GET",
            $this->authPoint . "/api/client?search={$this->email}&is_online=0&page=1&limit=100",
            [
                'headers'=> ['Authorization' => "Bearer " . $this->token]
            ]
        );
        $clientList = json_decode($response->getBody()->getContents(), true)['data']['data'];
        return $clientList;
    }
}